import Request from "./luch-request/index.js"
import tools from "@/common/AES.js";
const BASE_URL = "https://saasmall.duobakeji.com/api"


// const BASE_URL = "http://188.188.188.192:88/api"//冠男
// const BASE_URL = "http://188.188.188.148:9000/api"//少帅

const request = new Request()
/* 设置全局配置 */
request.setConfig((config) => {
    config.baseURL = BASE_URL
	// config.async = false
	config.dataType = "jsonp"
    return config
})

/* 请求以前拦截器 */
request.interceptors.request.use((config) => {
	config.header["Content-Type"] = "application/json"
	let jwt = uni.getStorageSync('jwt');
	
	// let jwt = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC8xODguMTg4LjE4OC4xOTI6ODhcL2FwaVwveGN4XC9sb2dpblhnaiIsImlhdCI6MTczMzI5ODI0OSwiZXhwIjoxNzMzOTAzMDQ5LCJuYmYiOjE3MzMyOTgyNDksImp0aSI6InVhTGJGQXhhT0J0ZmdKeTEiLCJzdWIiOjE4MjAsInBydiI6IjIzYmQ1Yzg5NDlmNjAwYWRiMzllNzAxYzQwMDg3MmRiN2E1OTc2ZjcifQ.GIkb1m2oW5BTGrI-4QEyUe9FCeg1pjmgCfPjerls9Io';
	config.header["Authorization"] = "bearer " +  jwt
	/* if(config.url == "/xcx/shopCart/addShopCart" || config.url == '/xcx/cateGoods/allGoods' || config.url == '/xcx/shopCart/addShopCart'){
		let jwt = uni.getStorageSync('jwt');
		config.header["Authorization"] = "bearer " + jwt
		 // config.header["Authorization"] = "bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC8xODguMTg4LjE4OC41OTo4OFwvYXBpXC94Y3hcL2xvZ2luIiwiaWF0IjoxNzEwOTIxMzI1LCJleHAiOjE3MTE1MjYxMjUsIm5iZiI6MTcxMDkyMTMyNSwianRpIjoiNUdSU0Q0OWVweHozaHdYeiIsInN1YiI6OTYyLCJwcnYiOiIyM2JkNWM4OTQ5ZjYwMGFkYjM5ZTcwMWM0MDA4NzJkYjdhNTk3NmY3In0.ruOOZZHZ3xFBy3BmGA0ejf7Y5J0lNgtUKZV3L5cdqpE"
	} */
	
	
	config.header = {
		...config.header
	}
	let agent_id = uni.getStorageSync('agent_id');
	let goodsId = uni.getStorageSync('goodsId');
	let pid = uni.getStorageSync('pid');
	// let giftId = uni.getStorageSync('giftId') != '' ?uni.getStorageSync('giftId') : 0;
	let mallId = uni.getStorageSync('mallId') != '' ?uni.getStorageSync('mallId') : 30;
	
	
	// 权限认证
	// config.header.token = uni.getStorageSync("token")
		if(config.method=="GET"){
			config.params.mall_id = mallId;
			config.params.agent_id =agent_id;
			config.params.goodsId = goodsId;
			config.params.pid = pid;
			config.params = {
				encrypt: tools.setAES(JSON.stringify(config.params)),
			}
			
		}else{
			config.data.mall_id = mallId;
			config.data.agent_id =agent_id;
			config.data.goodsId = goodsId;
			config.data.pid = pid;
			console.log(config.data)
			config.data = {
				encrypt: tools.setAES(JSON.stringify(config.data)),
			}
		}
		
	
	uni.showLoading({
		mask:true,
		title:"加载中"
	})
	return config
})

request.interceptors.response.use(async (res) => {
	/* 请求以后拦截器 */
	uni.hideLoading()
	console.log(res)
	return new Promise(function(reslove,reject){
		if(res.data.code==200){
			reslove(res.data)
		}else if(res.data.code==401){
			uni.showToast({
				title:res.data.msg,
				icon:'none',
				duration: 3000
			})
			setTimeout(() => {
			    uni.clearStorageSync();
			    uni.reLaunch({
			    	url: '/pages/active/index'
			    });
			}, 3000);
			
		} else{
			console.log('触发了')
			uni.showToast({
				title:res.data.msg,
				icon:'none'
			})
			// reject(res.data)
		}
	})
	
}, (error) => { // 请求错误
	console.log(error)
	uni.hideLoading()
	if (error.statusCode == 401) {
		// 登录态失效则清除token：没有携带token、token没法再刷新
        let msg = error.data.msg || error.data
        if(msg==null || msg==undefined) msg = '当前登录状态过期，请重新登录'
		uni.clearStorageSync()
		uni.showToast({
			title: msg,
			icon: 'none',
            success() {
               // location.reload()
			   // checkWeChatCode()
            }
		})
	}else{
        let msg = error.data.msg || error.data
		uni.showModal({
			title: '提示',
			content: msg,
			success: function (res) {
				if (res.confirm) {
					console.log('用户点击确定');
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});
		/* uni.showToast({
			title: msg,
			icon: 'none',
			duration: 2000
		}) */
	}
	return Promise.reject(error)
})

export default request